package com.atguigu.gmall.cart.controller;

import com.atguigu.gmall.cart.bean.CartInfo;
import com.atguigu.gmall.cart.service.CartInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 购物车表 用户登录系统时更新冗余 前端控制器
 * </p>
 *
 * @author zhangchen
 * @since 2024-07-24
 */
@RestController
@RequestMapping("/cart")
public class CartInfoController {

    @Autowired
    CartInfoService cartInfoService;

    //按主键查询   /detail/xxx    get
    @GetMapping("/detail/{id}")
    public CartInfo getDetail(@PathVariable("id") Long id) {
        CartInfo cartInfo = cartInfoService.getById(id);
        return cartInfo;
    }



    // 按条件查询列表 商品名称查询  /list?name=xxx  get
    @GetMapping("/list")
    public List<CartInfo> getList(@RequestParam(value = "name",required = false) String name) {
        List<CartInfo> cartInfoList = cartInfoService.list(new QueryWrapper<CartInfo>().like(name != null && name.trim().length() > 0, "sku_name", name));
        return cartInfoList;

    }


    // 保存+修改  /detail    POST
    @PostMapping("/detail")
    public  CartInfo saveDetail( @RequestBody CartInfo cartInfo){
        cartInfoService.saveOrUpdate(cartInfo);
        return cartInfo;
    }

    //  删除 /detail/xxx  DELETE
    @DeleteMapping("/detail/{id}")
    public String deleteDetail(@PathVariable("id") Long id){
        boolean isSuccess = cartInfoService.removeById(id);
        return  isSuccess?"success":"fail";

    }

}
